﻿using BLL;
using Inspinia_MVC5.Code;
using Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Inspinia_MVC5.Controllers
{
    [AllowCors]
    [SessionTimeOutFilter]
    public class PersonalController : Controller
    {
        // GET: Personal
        public ActionResult Index()
        {
            return View();
        }
        public ActionResult ManageWarnLog()
        {
            return View();
        }
        public ActionResult WarnLogSubscribe()
        {
            return View();
        }
        public JsonResult WellWarnLog(string wellid)
        {
            User2WellService User2WellService = new User2WellService();
            R_INFO_StructureService R_INFO_StructureService = new R_INFO_StructureService();
            string wellstr = Session["wellstr"].ToString();
            string userid = Session["userid"].ToString();
            int id = int.Parse(wellid);
            List<R_INFO_Structure> AllWarnLog = R_INFO_StructureService.All().ToList();
            if (wellstr == "0" || wellstr.Contains("-" + wellid + "-"))
            {
                User2Well user2well = User2WellService.FindAll(c => c.UserId == userid && c.Well.ID == id).FirstOrDefault();
                if (user2well == null)
                    return Json(new { status = "3", allwarnlog = AllWarnLog }, JsonRequestBehavior.AllowGet);
                return Json(new { status = "0", data = user2well.NotIgnore, allwarnlog = AllWarnLog }, JsonRequestBehavior.AllowGet);
            }
            else
            {
                return Json(new { status = "2" }, JsonRequestBehavior.AllowGet);
            }
        }
        public JsonResult UpdateWarnLog(string wellid, string warnlogstr)
        {
            User2WellService User2WellService = new User2WellService();
            string wellstr = Session["wellstr"].ToString();
            string userid = Session["userid"].ToString();
            int id = int.Parse(wellid);
            if (wellstr == "0" || wellstr.Contains("-" + wellid + "-"))
            {
                try
                {
                    User2Well user2well = User2WellService.FindAll(c => c.UserId == userid && c.Well.ID == id).FirstOrDefault();
                    if (user2well == null)
                    {
                        user2well.UserId = userid;
                        user2well.Well.ID = id;
                        user2well.NotIgnore = warnlogstr;
                        User2WellService.Add(user2well);
                    }
                    else
                    {
                        user2well.NotIgnore = warnlogstr;
                        User2WellService.Update(user2well);
                    }
                    return Json(new { status = "0" }, JsonRequestBehavior.AllowGet);
                }
                catch (Exception e)
                {
                    return Json(new { status = "3", data = e }, JsonRequestBehavior.AllowGet);
                }
            }
            else
            {
                return Json(new { status = "2" }, JsonRequestBehavior.AllowGet);
            }
        }
    }
}